package basic.lessons;

import java.awt.*;

public class MovingRects extends SimpleAnimationApplet2 {

  public void drawFrame(Graphics g) {

         // Draw one frame in the animation by filling in the background
         // with a solid red and then drawing a set of nested black
         // rectangles.  The frame number tells how much the first 
         // rectangle is to be inset from the borders of the applet.
         
      int width;    // Width of the applet, in pixels.
      int height;   // Height of the applet, in pixels.
      
      int inset;    // Gap between borders of applet and a rectangle.
                    //    The inset for the outermost rectangle goes
                    //    from 0 to 14 then back to 0, and so on,
                    //    as the frameNumber varies.
                    
      int rectWidth, rectHeight;   // The size of one of the rectangles.
                    
      width = getWidth();      // Find out the size of the drawing area.
      height = getHeight();

      g.setColor(Color.red);         // Fill the frame with red.
      g.fillRect(0,0,width,height);
      
      g.setColor(Color.black);         // Switch color to black.

      inset = getFrameNumber() % 15;   // Get the inset for the 
                                       //             outermost rect.
                                       
      rectWidth = width - 2*inset - 1;   // Set size of outermost rect.
      rectHeight = height - 2*inset - 1;
      
      while (rectWidth >= 0 && rectHeight >= 0) {
         g.drawRect(inset,inset,rectWidth,rectHeight);
         inset += 15;       // Rects are 15 pixels apart.
         rectWidth -= 30;   // Width decreases by 15 pixels 
                            //                 on left and 15 on right.
         rectHeight -= 30;  // Height decreases by 15 pixels 
                            //                 on top and 15 on bottom.
      }
      
   }  // end drawFrame()

}  // end class MovingRects